import { base64ToFile } from '@/assets/utils/index'
const baseUrl = ''
import router from '@/router/index.js'
// https://dh.bjbayes.com
export const useFetch = async (url, params) => {
  let token = window.localStorage.getItem('token')
  // if(!token){
  //   return router.push('/login')
  // }
  let res = await fetch(`${baseUrl}${url}`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
      Authorization: `Bearer ${token ? token : ''}`
    },
    body:params ? JSON.stringify(params) : ''
  })
  // let data = await res.json()
  if (res.status !== 200) {
    if (res.status == 401) {
      // throw new Error('未登录或登录已失效')
    //   return ElMessageBox.alert('未登录或登录已失效', '提示', {
    //     confirmButtonText: '确认'
    //   })
    router.push('/')
    return {
      code:401,
      message:'登录生效，重新登录'
    }
    }
    // throw new Error(res.statusText)
    // return ElMessageBox.alert(res.statusText, '提示', {
    //   confirmButtonText: '确认'
    // })
    return {
      code:404,
      message:'网络错误'
    }
  }
  let data = await res.json()
  if (data.token) {
    return data
  }
  // if (data.code !== 0) {
  //   // ElMessageBox.alert(data.msg, '提示', {
  //   //   confirmButtonText: '确认'
  //   // })
  //   return ''
  // }
  return data
  // }).then(res=>res.json()).then(data=>{
  //     console.log(data)
  // }).catch((e)=>{
  //     console.log('buggg');

  //     console.log(e)
  // })
}

export const fetchLogin = async (params) => {
  //登录
  let data = await useFetch('/zhensuo/admin/api/login', params)
  return data
}
export const fetchGenerateReport= async (params) => {
  //生成报告
  let data = await useFetch('/zhensuo/admin/api/generate_report', params)
  return data
}

///zhensuo/admin/api/start_check
export const fetchNativePay = async (params) => {
  //微信支付
  let data = await useFetch('/zhensuo/admin/api/native_pay', params)
  return data
}

export const fetchGetPayStat = async (params) => {
  //获取报告支付状态
  let data = await useFetch('/zhensuo/admin/api/get_pay_stat', params)
  return data
}
export const fetchListReport = async (params) => {
  //获取报告列表
  let data = await useFetch('/zhensuo/admin/api/list_report', params)
  return data
}
export const fetchStartCheck = async (params) => {
  //开始检测
  let data = await useFetch('/zhensuo/admin/api/start_check', params)
  return data
}

export const fetchTaskStat = async (params) => {
    //获取检测状态
    let data = await useFetch('/zhensuo/admin/api/get_stat', params)
    return data
}

export const fetchSetMaiLeft = async (params) => {
    //发送脉诊检测信息
    let data = await useFetch('/zhensuo/admin/api/set_maizhen_left', params)
    return data
}

export const fetchSetMaiRight = async (params) => {
    //发送脉诊检测信息
    let data = await useFetch('/zhensuo/admin/api/set_maizhen_right', params)
    return data
}

export const fetchListBingshi= async (params) => {
    //获取病史列表
    let data = await useFetch('/zhensuo/admin/api/list_bingshi', params)
    return data
}

export const fetchSetBingshi= async (params) => {
    //获取病史列表
    let data = await useFetch('/zhensuo/admin/api/set_bingshi', params)
    return data
}

///admin/api/list_wenzhen
export const fetchListWenzhen= async (params) => {
  //获取病史列表
  let data = await useFetch('/zhensuo/admin/api/list_wenzhen', params)
  return data
}
export const fetchListWenzhenMore= async (params) => {
  //获取病史列表
  let data = await useFetch('/zhensuo/admin/api/list_wenzhen_more', params)
  return data
}
export const fetchSetWenzhen= async (params) => {
  //获取病史列表
  let data = await useFetch('/zhensuo/admin/api/set_wenzhen', params)
  return data
}
export const fetchSetWenzhenMore= async (params) => {
  //获取病史列表
  let data = await useFetch('/zhensuo/admin/api/set_wenzhen_more', params)
  return data
}
/**上传图片 */
// /api/train/upload_oss_image
export const fetchUploadImg = async (base64Img) => {

  let formData = new FormData()
  formData.append('image', base64ToFile(base64Img))
  let res = await fetch(baseUrl + '/zhensuo/admin/api/upload_face_image', {
    method: 'POST',
    headers: {
      Authorization: `Bearer ${window.localStorage.getItem('token')}`
    },
    body: formData
  })

  // let data = await res.json()
  if (res.status !== 200) {
    if (res.status == 401) {
     
    }
    // throw new Error(res.statusText)
    return ''
  }
  let data = await res.json()
  if (data.token) {
    return data
  }
  if (data.code !== 0) {
    return ''
  }
  return data
}



